package com.share.shareplatform.sharegoods.controller;


import com.share.shareplatform.pojo.Type;
import com.share.shareplatform.pojo.exception.LogicException;
import com.share.shareplatform.sharegoods.service.TypeService;
import com.share.shareplatform.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/type")
@Validated
public class TypeController {

    @Autowired
    private TypeService typeService;

    @ApiOperation(value="商品类型添加",notes = "必需字段：name")
    @PostMapping
    @PreAuthorize("hasAuthority('userSuper')")
    public Result addOne(@RequestBody @Validated Type type) throws LogicException {

        Type one = typeService.findOneByName(type.getName());
        if(one!=null){
            throw new LogicException("该商品类型已经存在");
        }
        Long l = typeService.addOne(type);
        if(l==0){
            return Result.getInstance(505,"添加失败");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="商品类型修改",notes = "必需字段：id,name")
    @PutMapping
    @PreAuthorize("hasAuthority('userSuper')")
    public Result updateOne(@RequestBody @Validated Type type) throws LogicException {

        if(type.getId()==null){
            throw new LogicException("请指定商品类型Id");
        }

        Type one = typeService.findOneByName(type.getName());
        if(one!=null){
            throw new LogicException("该商品类型已经存在");
        }

        Integer flag = typeService.updateOne(type);
        if(flag==0){
            throw new LogicException("删除失败，该商品类型不存在");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="单个商品类型删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('userSuper')")
    public Result deleteOneById(@PathVariable Long id) {
        if(id==null||id<0){
            return Result.getInstance(505,"商品编号不合法");
        }
        Integer integer = typeService.deleteOneById(id);
        if(integer==0){
            return Result.getInstance(505,"删除失败");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="商品类型批量删除",notes = "其中ids为 'id-id-id' 这种类型的字符串")
    @DeleteMapping("/all/{ids}")
    @PreAuthorize("hasAuthority('userSuper')")
    public Result deleteAllByIds(@PathVariable String ids) {
        if(ids==null||ids.trim().equals("")){
            return Result.getInstance(505,"商品类型编号不合法");
        }
        String[] stringIds=ids.split("-");
        ArrayList<Long> arrayList=new ArrayList<>();
        try{
            for(String id:stringIds){
                arrayList.add(Long.parseLong(id));
            }
        }catch (Exception e){
            return Result.getInstance(505,"商品类型编号不合法");
        }

        Integer integer = typeService.deleteAllByIds(arrayList.toArray(new Long[arrayList.size()]));
        if(integer==0){
            return Result.getInstance(505,"删除失败");
        }
        return Result.getInstance(200,"删除了 "+integer+" 条数据");
    }

    @ApiOperation(value="商品类型查询，按name")
    @GetMapping("/name/{name}")
    @PermitAll
    public Result findOneByName( @PathVariable  String name) {
        if(name==null||name.equals("")){
            return Result.getInstance(505,"商品类型名称不合法");
        }
        Type type=typeService.findOneByName(name);
        if(type==null){
            return Result.getInstance(505,"not found");
        }

        return Result.getInstance(200,"success").put("type",type);
    }

    @ApiOperation(value="商品类型查询，按id")
    @GetMapping("/{id}")
    @PermitAll
    public Result findOneById(@PathVariable Long id) {
        Type type=typeService.findOneById(id);
        if(type==null){
            return Result.getInstance(505,"not found");
        }

        return Result.getInstance(200,"success").put("type",type);
    }

    @ApiOperation(value="查询所有商品类型")
    @GetMapping()
    @PermitAll
    public Result findAll(){
        List<Type> list = typeService.findAll();
        return Result.getInstance(200,"success").put("list",list);
    }
}
